package com.shanghaionstar.smart.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.shanghaionstar.R;

import java.util.List;
import java.util.Map;

/**
 * Created by fbx on 2015/8/24.
 */
public class AddDeviceAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<Map<String, Object>> list;
    private ICheckListener listener;
    private Map<Integer, Boolean> isSelected;
    private String tag;

    public AddDeviceAdapter(Context context, List<Map<String, Object>> deviceList, Map<Integer, Boolean> isSelected, ICheckListener listener, String tag){
        this.list = deviceList;
        this.isSelected = isSelected;
        this.listener = listener;
        this.tag = tag;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {

        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;

        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.smart_add_device_item, null);
            viewHolder.nameTV = (TextView) convertView.findViewById(R.id.item_tv);
            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.item_cb);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        if ("AddNewDeviceActivity".equals(tag)){
//            String ssid = (String) list.get(position).get(Code.KEY_WIFI_SSID);
//            String deviceName = MSmartSDK.getInstance().getDeviceManager().getDeviceNameBySSID(ssid);
//            viewHolder.nameTV.setText(deviceName);
        } else {
//            viewHolder.nameTV.setText((String) list.get(position).get(Code.KEY_DEVICE_NAME));
        }
        viewHolder.checkBox.setChecked(isSelected.get(position));
        viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean cu = !isSelected.get(position);
                for (Integer p : isSelected.keySet()) {
                    isSelected.put(p, false);
                }
                isSelected.put(position, cu);
                AddDeviceAdapter.this.notifyDataSetChanged();
                listener.onChecked(position, cu);
            }
        });


        return convertView;
    }
    static class ViewHolder {
        TextView nameTV;
        CheckBox checkBox;
    }

    public interface ICheckListener{
        void onChecked(int position, boolean isChecked);
    }

}
